<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class user_tasks extends CI_Controller {

 
	function __construct()
	{
		parent::__construct();
		
		// check if user logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}
		
		$this->load->library('form_validation');
		$this->load->model('hrm/user/task_model');	
		$this->load->model('common_model');	
	}
		
   function index()
   {
	   $meta['page_title'] = "Task Details";
	   $meta['dashboard'] = "dashboard";
	   //getting the data's required :
	   $data['pending_tasks'] = $this->task_model->get_pending_tasks(); 
	   
	   $data['achieved_tasks'] = $this->task_model->get_achieved_tasks();
		   
	   $data['completed_tasks'] = $this->task_model->get_completed_tasks();
	   
			
       $this->load->view('common/header', $meta);
	   $this->load->view('hrm/user/task',$data);
       $this->load->view('common/footer');
   }
   
   
   function create_task($edit_id = NULL)
   {
	   if ($this->ion_auth->in_group('manager')){
	   $this->form_validation->set_rules('user', 'User', 'required');
	   }
	    
		$this->form_validation->set_rules('project', 'Project Name', 'required');		
		$this->form_validation->set_rules('taskgroup', 'Task Groups', 'required');
		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('desc', 'Description', 'required');
		$this->form_validation->set_rules('start_date', 'Start Date', 'required');
		$this->form_validation->set_rules('end_date', 'End Date', 'required');
		$this->form_validation->set_rules('priority', 'Priority', 'required');		
		
		//$edit_id = null;
		$edit_id = $this->input->post('edit_id');
		if(!empty($edit_id)) {			
			$this->form_validation->set_rules('edit_id', 'Id', 'required');
			}
		
		if ($this->form_validation->run() == FALSE)
		{
			
		}
		else
		{						
			$for = ($this->input->post('user')) ? $this->input->post('user') : $this->session->userdata('user_id');			
			
			$edit_id = $this->input->post('edit_id');
			if(!empty($edit_id))
			{
			$update_insert = array(
			'project_id' => $this->input->post('project'),
			'milestone_id' => $this->input->post('milestone'),
			'taskgroup_id' => $this->input->post('taskgroup'),
			'user_id' => $for,
			'title' => $this->input->post('title'),
			'desc' => $this->input->post('desc'),
			'start_date' => $this->input->post('start_date'),
			'end_date' => $this->input->post('end_date'),
			'priority' => $this->input->post('priority'),
			);
			//print_r($update_insert);
			
			$this->task_model->update($update_insert,$edit_id);
				redirect('hrm/user_tasks');
			}
			else {

			$data = array(
			'project_id' => $this->input->post('project'),
			'milestone_id' => $this->input->post('milestone'),
			'taskgroup_id' => $this->input->post('taskgroup'),
			'user_id' => $for,
			'title' => $this->input->post('title'),
			'desc' => $this->input->post('desc'),
			'start_date' => $this->input->post('start_date'),
			'end_date' => $this->input->post('end_date'),
			'priority' => $this->input->post('priority'),
			);
				//print_r($data);	
			//exit;
			$insert_id = $this->task_model->create_task($data);
			$subtasks = $this->input->post('subtask');
			
			/* filters the empty array removes the empty array */
			$subtasks = array_filter($subtasks);

			//exit;	
			if(isset($subtasks)){
			
			foreach($subtasks as $subtask)			
					{
				$insert_subtasks = array(
				'task_id' => $insert_id,
				'user_id' => $for,
				'desc'=> $subtask,
				);				
		$this->task_model->create_subtask($insert_subtasks);
					}
				}
			}
			
			redirect('hrm/user_tasks/milestones_details/'.$this->input->post('project'));			
			//$this->load->view('formsuccess');
			
		}
	
		$this->data['users'] = $this->ion_auth->users()->result();	 
	    $meta['page_title'] = "Create a New Task";
		
		$this->data['projects'] = $this->task_model->get_all_projects();
		$this->data['milestones'] = $this->task_model->get_default_milestone();
		$this->data['taskgroups'] = $this->task_model->get_default_taskgroups();
		 $meta['dashboard'] = "dashboard";
		 $this->load->view('common/header', $meta);
	   	 $this->load->view('hrm/user/create-task',$this->data);
         $this->load->view('common/footer');
   }
   
   function edit($edit_id)
   {
		$this->data['users'] = $this->ion_auth->users()->result();	 
		$this->data['edit'] = $data = $this->task_model->edit($edit_id);
		$this->data['projects'] = $this->task_model->get_all_projects();
		$this->data['milestones'] = $this->task_model->get_milestones();
		$this->data['taskgroups'] = $this->task_model->get_taskgroups();
		
		//print_r($this->data['users']);exit;
		 $meta['page_title'] = "Edit Task";	  
	  $meta['dashboard'] = "dashboard";
	   $this->load->view('common/header', $meta);
	   $this->load->view('hrm/user/create-task',$this->data);
       $this->load->view('common/footer');
   }
   
   function task_detail($edit_id)
   {
	$meta['page_title']= "Task Details";

	$data['tasks'] = $this->task_model->get_task($edit_id);
	$data['notes'] = $this->task_model->get_note_by_id($edit_id);
	$data['task_id'] = $this->uri->segment(4);
	
	$data['subtasks'] = $this->task_model->get_subtask($edit_id);
	
	/*$data['subtasks_achieved'] = $this->task_model->get_subtask_achieved($edit_id);*/
	$meta['dashboard'] = "dashboard";
		//redirect('/','refresh');
		$this->load->view('common/header', $meta);
		$this->load->view('hrm/user/task-detail',$data);
		$this->load->view('common/footer');	
   }
   
  function delete_task($task_id,$milestone_id)
   {
	 $this->task_model->delete_task($task_id);
			$this->session->set_flashdata('note_message', 'Note delted successfully');
			redirect('hrm/user_tasks/milestones_details/'.$milestone_id); 
   }
   
   /* notes function starts here */
   function add_note()
   {
	 //echo  $task_id = nl2br($_REQUEST['note']);
	
	
	  $insert=array(
		   'user_id' => $this->session->userdata('user_id'), 
		   'task_id' =>	$_REQUEST['taskid'],
		   'note' 	 =>	nl2br($_REQUEST['note']),
		   'date'	 => date('Y-m-d')
	  	);
		
		$this->task_model->add_note($insert);
		$this->session->set_flashdata('note_message', 'Note succesfully added');
		}
		
		function delete_note($note_id,$task_id)
		{
			
			$this->task_model->delete_note($note_id);
			$this->session->set_flashdata('note_message', 'Note delted successfully');
			
			redirect('hrm/user_tasks/task_detail/'.$task_id,'refresh');
		}
		
		function task_achieved()
		{
			// $task_id = $_REQUEST['taskid'];
			$update = array(			
			'is_achieved' => 1
			);
			$check = array(
			'id' => $_REQUEST['taskid']
			);
			$this->task_model->is_achieved($update,$check);			
		}
		
			function task_unachieved()
		{
			$update = array(			
			'is_achieved' => 0
			);
			$check = array(
			'id' => $_REQUEST['taskid']
			);
			//print_r($check);
			//exit;
			$this->task_model->task_unachieved($update,$check);
		}
		
		function task_closed()
		{
			// $task_id = $_REQUEST['taskid'];
			$update = array(			
			'is_completed' => 1
			);
			$check = array(
			'id' => $_REQUEST['taskid']
			);
			$this->task_model->task_closed($update,$check);
			
		}
		
		function task_open()
		{
			// $task_id = $_REQUEST['taskid'];
			$update = array(			
			'is_completed' => 0
			);
			$check = array(
			'id' => $_REQUEST['taskid']
			);
			$this->task_model->task_open($update,$check);
			
		}
		
		function check_is_achieved()
		{
			$task_id = $_REQUEST['taskid'];
			$value = $this->task_model->check_is_achieved($task_id);
			echo $value['is_achieved'];	
			
		}
		
		function check_is_completed()
		{
		$task_id = $_REQUEST['taskid'];
		$value = $this->task_model->check_is_completed($task_id);
			echo $value['is_completed'];	
		}
		
		// sub tasks functions 		
		function create_subtask($task_id)
		{
			//echo $this->input->post('subtask');
			//exit;
			$insert_subtasks = array(
				'task_id' => $task_id,
				'user_id' => $this->session->userdata('user_id'),
				'desc'=> $this->input->post('subtask'),
				'priority' => $this->input->post('priority'),
				'is_achieved'=>0
				);
				$value = $this->task_model->create_subtask($insert_subtasks);
			//	$this->load->view('common/header');
			redirect('hrm/user_tasks/task_detail/'.$task_id);
			//$this->form_validation->set_rules('subtask', 'Subtask', 'required');
		}
		
		function create_subtask_heading($task_id)
		{
			$insert_subtasks = array(
				'task_id' => $task_id,
				'user_id' => $this->session->userdata('user_id'),
				'desc'=> $this->input->post('subtask'),				
				'is_heading' => 1,
				);
				$value = $this->task_model->create_subtask($insert_subtasks);
			//	$this->load->view('common/header');
			redirect('hrm/user_tasks/task_detail/'.$task_id);
			//$this->form_validation->set_rules('subtask', 'Subtask', 'required');
		}
		
		function delete_subheading($id,$task_id)
		{
		//echo $id,$task_id;
			//exit;		
		echo  $value = $this->task_model->delete_notes(1);
		//redirect('user/tasks/task_detail/'.$task_id);
		}
			
		function subtask_achieved()
		{
			 $subtask_id = $_REQUEST['subtask_id'];
			echo  $value = $this->task_model->subtask_achieved($subtask_id);
		}
		
		function subtask_unachieved()
		{
			 $subtask_id = $_REQUEST['subtask_id'];
			 $value = $this->task_model->subtask_unachieved($subtask_id);
		}
		
		function check_subtask_is_achieved()
		{
			$subtask_id = $_REQUEST['subtask_id'];
			$value = $this->task_model->check_subtask_is_achieved($subtask_id);
			echo $value['is_achieved'];
		}
		
		function delete_subtask($task_id,$delete_id)
		{
		 $this->task_model->delete_subtask($delete_id);	
		//$this->db->delete('mytable', array('id' => $delete_id)); 
		redirect('hrm/user_tasks/task_detail/'.$task_id,'refresh');
		}
		
		function edit_subtask($id)
		{
			$data['subtasks'] = $this->task_model->edit_subtask($id);
		$this->load->view('hrm/user/edit-subtask',$data);			
		}

	function update_subtask()
	{
	$update = array(
			'desc'=> $this->input->post('desc'),
			'priority' =>$this->input->post('priority')
			);
			
	$id =  $this->input->post('id');
	$task_id = $this->input->post('taskid');
	$this->task_model->update_subtask($update,$id);	
	redirect('hrm/user/tasks/task_detail/'.$task_id,'refresh');
	}
		/* Project function starts here*/
		
		function projects()
		{
	$data['projects'] = $this->task_model->get_all_projects();
		$meta['page_title'] = "Projects";
		$this->load->view('common/header',$meta);	
		$this->load->view('hrm/user/projects',$data);	
		$this->load->view('common/footer');	
		}
		
		function create_project()
		{
			$this->form_validation->set_rules('name', 'Name', 'required|is_unique[projects.name]');
			$this->form_validation->set_rules('desc', 'Description', 'required');
			$this->form_validation->set_rules('start_date', 'Start Date', 'required');
			$this->form_validation->set_rules('due_date', 'Due Date', 'required');
			
			if ($this->form_validation->run() == FALSE)
			{
				$data['projects'] = $this->task_model->get_all_projects();
		$meta['page_title'] = "Projects";
		$meta['dashboard'] = "dashboard";
		$this->load->view('common/header',$meta);	
		$this->load->view('user/user/projects',$data);	
		$this->load->view('common/footer');			
			}
		else{
			$insert = array(
			'name' => $this->input->post('name'),
			'desc' => $this->input->post('desc'),
			'start_date' => $this->input->post('start_date'),
			'end_date' => $this->input->post('due_date'),
			'created_on' => date('Y-m-d'),
			'created_by' => $this->session->userdata('user_id'),
			'is_completed' => 0
			);
			//print_r($insert);
			//exit;
			$this->task_model->create_project($insert);		
			redirect('user/tasks/projects','refresh');			
			}			
		}
		
		/* Project function ends here*/
		
		/* Milestone function starts here*/
		function milestones($project_id)
		{
		
		$data['projects'] = $this->task_model->get_project_name_by_id($project_id);
		$data['milestones'] = $this->task_model->get_milestone_by_project_id($project_id);
		
		$data['default_milestones'] = $this->task_model->get_default_milestone($project_id);
		$meta['dashboard'] = "dashboard";	
		$meta['page_title'] = "Projects";
		$this->load->view('common/header',$meta);	
		$this->load->view('user/milestones',$data);	
		$this->load->view('common/footer');		
		}
		
		function default_milestone()
		{
		$data['milestones'] = $this->task_model->get_default_milestone();	
		$meta['page_title'] = "Projects";
		$meta['dashboard'] = "dashboard";
		$this->load->view('common/header',$meta);	
		$this->load->view('user/default-milestones',$data);	
		$this->load->view('common/footer');	
		}
		
		function create_default_milestone()
		{
			$this->form_validation->set_rules('name', 'Name', 'required|is_unique[milestones.name]');
			$this->form_validation->set_rules('desc', 'Description', 'required');			
			
			if ($this->form_validation->run() == FALSE)
			{
		redirect('user/tasks/default_milestone');
			}
			else{
				$insert = array(
				'project_id' => 0,
				'name' => $this->input->post('name'),
				'desc' => $this->input->post('desc'),
				'created_on' => date('Y-m-d'),
				'created_by' => $this->session->userdata('user_id'),
				'is_default' => 1,				
				);
				$this->task_model->create_milestone($insert);
				
				redirect('user/tasks/default_milestone');
			}
		}
	function create_milestone($project_id)
		{
			/*$data['projects'] = $this->task_model->get_project_name_by_id($project_id);			
		$meta['page_title'] = "Create MileStone";
		$this->load->view('common/header',$meta);	
		$this->load->view('user/create-milestones',$data);	
		$this->load->view('common/footer');	*/
		
		
			$this->form_validation->set_rules('name', 'Name', 'required|is_unique[projects.name]');
			$this->form_validation->set_rules('desc', 'Description', 'required');
					
			
			if ($this->form_validation->run() == FALSE)
			{
				$data['projects'] = $this->task_model->get_project_name_by_id($project_id);
							
		$meta['page_title'] = "Create MileStone";
		$data['milestones'] = $this->task_model->get_milestone_by_project_id($project_id);
		$meta['dashboard'] = "dashboard";
		$this->load->view('common/header',$meta);	
		$this->load->view('user/create-milestones',$data);	
		$this->load->view('common/footer');	
			}
			
			else
			{				
			$project_id = $this->input->post('project_id');
			$insert = array(
			'project_id' => $this->input->post('project_id'),
			'name' => $this->input->post('name'),
			'desc' => $this->input->post('desc'),			
			'created_on' => date('Y-m-d'),
			'created_by' => $this->session->userdata('user_id'),
			'is_default' => 0,
			);
			//print_r($insert);
					
			$this->task_model->create_milestone($insert);
			redirect('user/tasks/milestones/'.$project_id,'refresh');	
		}
	}
	
	function get_milestone_by_project_id()
	{
		$project_id = $_REQUEST['project_id'];
	 	$default_options =  $this->task_model->get_default_milestone();
		$custom_options =  $this->task_model->get_milestone_by_project_id($project_id);
			
			echo '<select class="chzn-select" id="milestone1">';
	foreach($default_options as $default_option)
			{
	echo '<option value="'.$default_option->id.'">'.$default_option->name.'</option>';	
			}
			foreach($custom_options as $custom_option)
			{
	echo '<option value="'.$custom_option->id.'">'.$custom_option->name.'</option>';	
			}			
	echo '</select>';
		
		
	}
	
	
	/* default taskgroup */ 
	
	function get_taskgroup_by_milestone_id()
	{
		$milestone_id = $_REQUEST['milestone_id'];
	 	$default_options =  $this->task_model->get_default_taskgroup();
		$custom_options =  $this->task_model->get_taskgroup_by_milestone_id($project_id);
			
			echo '<select class="chzn-select" id="">';
	foreach($default_options as $default_option)
			{
	echo '<option value="'.$default_option->id.'">'.$default_option->name.'</option>';	
			}
			foreach($custom_options as $custom_option)
			{
	echo '<option value="'.$custom_option->id.'">'.$custom_option->name.'</option>';	
			}			
	echo '</select>';
		
		
	}
		
		function default_taskgroup()
		{
		$data['taskgroups'] = $this->task_model->get_default_taskgroups();	
		$meta['page_title'] = "Projects";
		$this->load->view('common/header',$meta);	
		$this->load->view('user/default-task-groups',$data);	
		$this->load->view('common/footer');	
		}
		
	function create_default_taskgroup()
		{
			$this->form_validation->set_rules('name', 'Name', 'required|is_unique[milestones.name]');
			$this->form_validation->set_rules('desc', 'Description', 'required');			
			
			if ($this->form_validation->run() == FALSE)
			{
				//redirect('user/tasks/default_taskgroup');	
		$data['taskgroups'] = $this->task_model->get_taskgroups();	
		$meta['page_title'] = "Projects";
		$this->load->view('common/header',$meta);	
		$this->load->view('user/default-task-groups',$data);	
		$this->load->view('common/footer');		
			}
			else{
				$insert = array(
				'project_id' => 0,
				'milestone_id' => 0,
				'name' => $this->input->post('name'),
				'desc' => $this->input->post('desc'),
				'created_on' => date('Y-m-d'),
				'created_by' => $this->session->userdata('user_id'),
				'is_default' => 1,				
				);
				$this->task_model->create_taskgroups($insert);
				
				redirect('user/tasks/default_taskgroup');
			}
		}

/* create custom task groups*/

function custom_taskgroup($project_id,$milestone_id)
{
$data['default_taskgroups'] = $this->task_model->get_default_taskgroups();
$data['custom_taskgroups'] = $this->task_model->get_taskgroup_by_id($project_id,$milestone_id);

$data['project_id'] = $project_id;
$data['milestone_id'] = $milestone_id;

$meta['page_title'] = "Task Groups";
$this->load->view('common/header',$meta);
$this->load->view('user/create-task-group',$data);
$this->load->view('common/footer');	
}

function create_custom_taskgroup($project_id,$milestone_id)
{
	$this->form_validation->set_rules('name', 'Name', 'required|is_unique[projects.name]');
			$this->form_validation->set_rules('desc', 'Description', 'required');
			if ($this->form_validation->run() == FALSE)
			{
				$data['default_taskgroups'] = $this->task_model->get_default_taskgroups();
				$data['custom_taskgroups'] = $this->task_model->get_taskgroup_by_id($project_id,$milestone_id);
				
				$data['project_id'] = $project_id;
				$data['milestone_id'] = $milestone_id;

				$meta['page_title'] = "Task Groups";
				$this->load->view('common/header',$meta);
				$this->load->view('user/create-task-group',$data);
				$this->load->view('common/footer');		
			}
			else{
				
				$insert = array(
				'project_id'=> $project_id,
				'milestone_id'=>$milestone_id,
				'name' => $this->input->post('name'),
				'desc' =>$this->input->post('desc'),
				 'created_on' => date('Y-m-d'),
				 'created_by' => $this->session->userdata('user_id'),
				 'is_default' => 0
				);
				//print_r($insert);
				//exit;
				$this->task_model->create_taskgroups($insert);
				
				redirect('user/tasks/custom_taskgroup/'.$project_id.'/'.$milestone_id,'refresh');
				}
			}
			
			/* project_details function starts here */
			
			function projects_details()
			{
				$meta['page_title'] = "Project details";
				$data['projects'] = $this->task_model->get_all_projects();
				$this->load->view('common/header',$meta);
				$this->load->view('user/projects-details',$data);
				$this->load->view('common/footer');		
			}
			
			function milestones_details($project_id)
			{
				$meta['page_title'] = "Project details";
				$data['projects'] = $this->task_model->get_project_name_by_id($project_id);
				$data['milestones'] = $this->task_model->get_default_milestone();
				
				$data['taskgroups'] = $this->task_model->get_default_taskgroups();
				
//$tasks = $this->task_model->get_tasks_by_taskgroup_and_project($data['taskgroups']->id,$data['projects']['id']);
				
				//print_r($data['taskgroups']['id']);
				//print_r($data['projects']['id']);
				//$data['tasks'] = $this->task_model->get_tasks_by_taskgroup($tasksgroup_id);
				
				/* calculate the completed %*/
				//get the subtask count
				
				//$data['subtasks']= $this->task_model->get_subtask();
				
				//exit;
				
				$this->load->view('common/header',$meta);
				$this->load->view('user/milestones-details',$data);
				$this->load->view('common/footer');		
			}
			
		function sorting_subtasks()
			{
					
		$action = mysql_real_escape_string($_POST['action']);
		$updateRecordsArray = $_POST['recordsArray'];
		
		$this->task_model->sorting_subtask($action,$updateRecordsArray);
		
			} 
			
			function completed_percentage()
			{
			//$completed = 0;
			 $completed = $_REQUEST['comp'];	
			//exit;
$this->task_model->completed_percentage($completed ,$_REQUEST['id']);
			}
			

}
?>